package main

import (

	"strings"
	"github.com/boltdb/bolt"

	"match-spider/util/boltdb"
	"fmt"
	"regexp"
	"github.com/json-iterator/go"
	"match-spider/util/pg"
	"time"
)

func teamId(fn func([]interface{},int))  {
	i:=0
	boltdb.ScanTx("matchs-2017", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")

				reg, _ := regexp.Compile("\\[[\\S\\s]+?]")
				league:=s[1]
				t1:=s[3]
				t2:=s[5]
				t1= reg.ReplaceAllString(t1, "")
				t2= reg.ReplaceAllString(t2, "")
				t1=strings.TrimSpace(t1)
				t2=strings.TrimSpace(t2)
				boltdb.Get("asia-2017",league,string(k), func(asia string) {
					if !strings.Contains(asia,"澳门"){
						return
					}
					i++

					fmt.Println(i)
					//16913
					
					boltdb.Get("bf-2017", league, string(k), func(text string) {
						fmt.Println(text)
						/*
						boltdb.Get("europe-2016", league, string(k), func(europe string) {
							boltdb.Get("overunder-2016", league, string(k), func(overunder string) {
								//matchInfo(string(k),text)
								fmt.Println(overunder)
								i++
								fmt.Println(i)
							})
						})*/

					})
				})

				return nil
			})
			return nil
		})

	})

}
func storeMatch(s []string)  {
	//[1160129 意甲 16-02-04 03:45 [8]拉齐奥 0 那不勒斯[1]  2 受平/半 2.5]

	reg, _ := regexp.Compile("\\[[\\S\\s\\d]+?]")
	reg2, _ := regexp.Compile("\\d+")

	league:=s[1]
	t1:=s[3]
	t2:=s[5]
	homeRank:=string(reg.Find([]byte(t1)))
	homeRank=strings.Replace(homeRank,"[","",-1)
	homeRank=strings.Replace(homeRank,"]","",-1)
	homeRank=string(reg2.Find([]byte(homeRank)))
	if homeRank==""{
		homeRank="0"
	}
	awayRank:=string(reg.Find([]byte(t2)))
	awayRank=strings.Replace(awayRank,"[","",-1)
	awayRank=strings.Replace(awayRank,"]","",-1)
	awayRank=string(reg2.Find([]byte(awayRank)))
	if awayRank==""{
		awayRank="0"
	}
	t1= reg.ReplaceAllString(t1, "")
	t2= reg.ReplaceAllString(t2, "")
	homeName:=strings.TrimSpace(t1)
	awayName:=strings.TrimSpace(t2)
	sql:="select * from t_team where name=$1"

	home:=pg.FindFirst(sql,strings.Replace(homeName,"(中)","",-1))
	away:=pg.FindFirst(sql,strings.Replace(awayName,"(中)","",-1))
	startAt:="20"+s[2]
	startDate,_:=time.Parse("2006-01-02 15:04",startAt)
	hour:=startDate.Hour()
	createdDate:=""
	if hour>11{
		createdDate=startDate.Format("2006-01-02")
	}else{
		createdDate=startDate.AddDate(0,0,-1).Format("2006-01-02")
	}

	data:=[]interface{}{s[0],homeName,home["id"],awayName,away["id"],s[4],s[6],startAt,league,0,createdDate,0,0,homeRank,awayRank,-1}
	fmt.Println(data)

	//pg.UpdateOrDelete("delete from t_match where id=$1",s[0])
	sql = "insert into t_match(id,home,home_id,away,away_id,home_score,away_score,start_at,league,league_id,create_date," +
	"is_jc,jc_number,home_rank,away_rank,state) values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)"
	pg.Insert(sql,data...)

}
func matchInfo(id string,content string)  {

	if strings.Compare(strings.TrimSpace(content), "") == 0 {
		//todo

	} else {
		json := jsoniter.ConfigCompatibleWithStandardLibrary
		//伤停情况
		st := ""
		if strings.Contains(content, "Home_S") {
			ss := strings.Split(content, "listAway")[1]
			sss := strings.Split(ss, "$$")[2]
			data := make(map[string]interface{})
			json.Unmarshal([]byte(sss), &data)
			homeS := data["Home_S"].([]interface{})
			guestS := data["Guest_S"].([]interface{})
			homeT := data["Home_T"].([]interface{})
			guestT := data["Guest_T"].([]interface{})

			stMap := map[string]interface{}{
				"hs": homeS,
				"gs": guestS,
				"ht": homeT,
				"gt": guestT,
			}
			stBytes, _ := json.Marshal(stMap)
			st = string(stBytes)
		}

		prefix := "$$"

		s:=strings.Split(content, prefix)

		pointData := s[0]
		if s[1]!=""{
			pointData +="$$"+s[1]
		}
		pointData=strings.Replace(pointData,string('\uFEFF'),"",-1)
		betData := s[3]

		homeMatchs:=s[4]
		awayMatchs:=s[5]

		scheduleMatch := s[6]+"$$"+s[7]

		sql := "insert into t_match_info(match_id,point_data,bet_data,st,schedule,home_matchs,away_matchs) values($1,$2,$3,$4,$5,$6,$7)"
		fmt.Println(id,"======>")
		pg.Insert(sql,id,pointData,betData,st,scheduleMatch,homeMatchs,awayMatchs)

	}
}

func main()  {
	teamId(nil)

}